package com.climber.android.commonres.helper

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import com.climber.android.commonres.R
import com.google.android.material.bottomsheet.BottomSheetDialog
import io.library.android.adapter.LQRAdapterForRecyclerView
import io.library.android.adapter.LQRViewHolderForRecyclerView
import io.library.android.recyclerview.LQRRecyclerView

/**
 * Created by Mikes at 2020-03-30 17:33
 */
object BottomSheetDialogHelper {
    fun showBottomSingleChoiceListDialog(context: Context, title : String, itemArray : List<String>, callback : ((index : Int)->Unit)){
        val dialog = BottomSheetDialog(context)

        val contentView = LayoutInflater.from(context).inflate(R.layout.com_bottom_dialog_list_single_choice, null)

        val titleLabel = contentView.findViewById<TextView>(R.id.titleLabel)
        titleLabel.text = title

        var choosedItemIndex = 0
        val rvChoice = contentView.findViewById<LQRRecyclerView>(R.id.rvChoice)
        rvChoice.adapter = object : LQRAdapterForRecyclerView<String>(context, itemArray.toMutableList(), R.layout.com_bottom_dialog_list_single_choice_item){
            override fun convert(helper: LQRViewHolderForRecyclerView, item: String, position: Int) {
                helper.setText(R.id.itemTitleLabel, item)

                val itemChoice = helper.getView<CheckBox>(R.id.itemChoice)
                itemChoice.setOnCheckedChangeListener(null)
                itemChoice.isChecked = choosedItemIndex == position
                itemChoice.setOnCheckedChangeListener { _, isChecked ->
                    choosedItemIndex = position
                    notifyDataSetChanged()
                }

                helper.getView<TextView>(R.id.itemTitleLabel).onClick {
                    itemChoice.performClick()
                }
            }
        }

        val confirmBtn = contentView.findViewById<TextView>(R.id.confirmBtn)
        confirmBtn.onClick {
            callback.invoke(choosedItemIndex)

            dialog.dismiss()
        }

        dialog.setContentView(contentView)

        dialog.show()
    }

    fun showOnlyTitleDialog(context: Context, title : String, callback : ((confirm : Boolean)->Unit)){
        val dialog = BottomSheetDialog(context)

        val contentView = LayoutInflater.from(context).inflate(R.layout.com_bottom_dialog_only_title, null)

        val titleLabel = contentView.findViewById<TextView>(R.id.titleLabel)
        titleLabel.text = title

        val confirmBtn = contentView.findViewById<TextView>(R.id.confirmBtn)
        val cancelBtn = contentView.findViewById<TextView>(R.id.cancelBtn)
        confirmBtn.onClick {
            callback.invoke(true)

            dialog.dismiss()
        }

        cancelBtn.onClick {
            callback.invoke(false)

            dialog.dismiss()
        }

        dialog.setContentView(contentView)

        try {
            // 改变BottomSheetDialog背景色为透明
            val parent = contentView.parent as ViewGroup
            parent.setBackgroundResource(android.R.color.transparent)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        dialog.show()
    }

    fun showSingleTextChoiceDialog(context: Context, actions : List<String>, callback : ((item : Int)->Unit)){
        val dialog = BottomSheetDialog(context)

        val contentView = LayoutInflater.from(context).inflate(R.layout.com_bottom_dialog_list_single_text, null)

        val rvChoice = contentView.findViewById<LQRRecyclerView>(R.id.rvChoice)
        val adapter = object : LQRAdapterForRecyclerView<String>(context, actions, R.layout.com_bottom_dialog_list_single_text_item){
            override fun convert(helper: LQRViewHolderForRecyclerView, item: String, position: Int) {
                helper.setText(R.id.itemLabel, item)
            }
        }
        rvChoice.adapter = adapter
        adapter.setOnItemClickListener { _, _, _, position ->
            callback.invoke(position)
            dialog.dismiss()
        }

        val cancelBtn = contentView.findViewById<TextView>(R.id.cancelBtn)
        cancelBtn.onClick {
            dialog.dismiss()
        }

        dialog.setContentView(contentView)

        try {
            // 改变BottomSheetDialog背景色为透明
            val parent = contentView.parent as ViewGroup
            parent.setBackgroundResource(android.R.color.transparent)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        dialog.show()
    }
}